package com.wwwarehouse.financialcenter.fragment.moneywarehouse.homepage;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.carddesk.CardDeskMessageStableCardBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.financialcenter.R;
import com.wwwarehouse.financialcenter.bean.bindingcard.MoneyHouseInfomationBean;
import com.wwwarehouse.financialcenter.constant.FinancialConstant;
import com.wwwarehouse.financialcenter.fragment.base.MyBaseViewPagerFragment;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

import static com.wwwarehouse.financialcenter.constant.FinancialConstant.PATH_MONEYHOUSE;

/**
 * @author Joseph on 2017/11/14.
 *         <p/>
 *         选择钱仓
 */
@Route(path = PATH_MONEYHOUSE)
public class MoneyHouseFragment extends MyBaseViewPagerFragment {

    private CardDeskMessageStableCardBean.TasksBean tasksBean;

    @Override
    public void setTitle() {
        if (peekFragment() instanceof MoneyHouseFragment)
            mActivity.setTitle(getString(R.string.financial_center_enterprise_choose_money_house));
    }

    @Override
    protected void init(View view) {
        super.init(view);
        Bundle bundle = getArguments();
        if (bundle != null) {
            tasksBean = bundle.getParcelable("data");
        }

        httpPost(FinancialConstant.LISTACCOUNT, new HashMap(), 0, false, "");
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        if (what == 0) {
            if (TextUtils.equals("0", commonClass.getCode())) {
                List<MoneyHouseInfomationBean.DataBean> dataBeen = JSON.parseArray(commonClass.getData().toString(), MoneyHouseInfomationBean.DataBean.class);
                Bundle b = new Bundle();
                b.putSerializable("moneyHomeData", (Serializable) dataBeen);
                b.putParcelable("tasksBean", tasksBean);
                if (dataBeen != null && dataBeen.size() == 1) {
                    popFragment();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("moneyHomeData", dataBeen.get(0));
                    bundle.putSerializable("moneyHomeDataList", (Serializable) dataBeen);
                    bundle.putParcelable("tasksBean", tasksBean);
                    MoneyHomePageFragment moneyHomePageFragment = new MoneyHomePageFragment();
                    moneyHomePageFragment.setArguments(bundle);
                    pushFragment(moneyHomePageFragment);
                } else if (dataBeen != null && dataBeen.size() > 1) {
                    SetFragmentAndPage(dataBeen.size(), 9, new ChooseMoneyHouseFragment().getClass().getName(), b);
                }
            }
        }

    }

    @Override
    public void onReLoad(int code) {
        httpPost(FinancialConstant.LISTACCOUNT, new HashMap(), 0, false, "");
    }
}
